package com.link2room.base.facade.impl;

import com.link2room.bumblebee.dto.result.base.PmsReservationOptionDto;
import com.link2room.bumblebee.entity.PmsReservationOption;
import com.link2room.bumblebee.util.BumblebeeUtil;
import com.link2room.common.constant.BaseConstant;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.dao.ICommonDao;
import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.dto.result.base.HotelBaseInfoDto;
import com.link2room.common.dto.result.base.L2RGroupBaseInfoDto;
import com.link2room.common.dto.result.base.L2RGroupPayOptionDto;
import com.link2room.common.entity.SysOptions;
import com.link2room.common.jpa.criteria.CriteriaUtil;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.redis.RedisUtil;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.CommonUtil;
import com.link2room.common.util.JacksonUtil;
import com.link2room.common.util.StringUtil;
import com.link2room.entity.*;
import com.link2room.ironhide.entity.L2RGroupBaseinfo;
import com.link2room.ironhide.entity.L2RHotelBaseinfo;
import com.link2room.base.facade.IMappingServiceCacheFacadeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Created by Nakisa on 2016/10/20.
 */
@Component("mappingServiceCacheFacadeService")
public class MappingServiceCacheFacadeServiceImpl implements IMappingServiceCacheFacadeService {
    @Resource
    private ICommonService commonService;
    @Resource
    private ICommonDao commonDao;
    @Resource
    private RedisUtil redisUtil;
    private static Logger logger = LoggerFactory.getLogger(MappingServiceCacheFacadeServiceImpl.class);

    @Override
    public BaseResultDto refeshMappingServiceCache() throws Exception {
        logger.info("###refeshMapping MappingDescription###");
        this.cache();
        BaseResultDto resultDto = new BaseResultDto();
        resultDto.setResultCode(0);
        resultDto.setResultMessage("success");
        return resultDto;
    }

    @Override
    public void cache() throws Exception {
        logger.info("###加载MappingDescription###");
      //  this.setMappingDescription();
        logger.info("###加载MappingServiceHost###");
        //this.setMappingServiceHost();
        logger.info("###加载MappingService###");
        //this.setMappingService();
        logger.info("###L2RGroupBaseinfo###");
        this.setL2RGroupBaseinfo();
        logger.info("###L2RHotelBaseinfo###");
        this.setL2RHotelBaseinfo();
        logger.info("###加载MappingDescription###");
        //this.setMappingDescription();
        logger.info("###加载SysOption###");
        this.setSysoptionService();
        logger.info("###加载PmsReservationOption###");
        this.setPmsReservationOptions();
    }
    private void setMappingDescription()throws Exception{
        MappingDescription md = new MappingDescription();
        long count = commonDao.getCount(MappingDescription.class, CriteriaUtil.getCriteriaAllEQ(md), null);
        if(count == 0){
            return;
        }else{
            long size =  count/50;
            for(int i = 0; i <= size ; i ++){
                QueryResult<MappingDescription> res = commonDao.findAll(MappingDescription.class, CriteriaUtil.getCriteriaAllEQ(md), (i+1) , 50, null, null);
                if(res.getResultlist() != null && res.getResultlist().size() != 0){
                    List<MappingDescription> mdList = res.getResultlist();
                    for(int j = 0 ; j < mdList.size() ; j++){
                        MappingDescription md1 = mdList.get(j);
                        String key = md1.getKey(md1.getClassName(),md1.getField(),md1.getItem());
                        redisUtil.set(key,md1.getName());
                    }
                }
            }
        }
    }

    /**
     * 加载host到redis
     * @throws Exception
     */
    private void setMappingServiceHost()throws Exception{
        Map<String,String> resMap = new HashMap<>();
        MappingServiceHost ms = new MappingServiceHost();
        long count = commonDao.getCount(MappingServiceHost.class, CriteriaUtil.getCriteriaAllEQ(ms), null);
        if(count == 0){
            return ;
        }else{
            long size =  count/50;
            for(int i = 0; i <= size ; i ++){
                QueryResult<MappingServiceHost> res = commonDao.findAll(MappingServiceHost.class, CriteriaUtil.getCriteriaAllEQ(ms),(i+1) , 50, null, null);
                if(res.getResultlist() != null && res.getResultlist().size() != 0){
                    List<MappingServiceHost> mdList = res.getResultlist();
                    for(int j = 0 ; j < mdList.size() ; j++){
                        MappingServiceHost ms1 = mdList.get(j);
                        redisUtil.set(ms1.getServiceType(),ms1.getValue());
                    }
                }
            }
        }
    }

    /**
     * 加载mappingservice到redis  hote+value
     * @param hostMap
     * @throws Exception
     */
    @Deprecated
    private void setMappingService(Map hostMap)throws Exception{
        MappingService ms = new MappingService();
        long count = commonDao.getCount(MappingService.class, CriteriaUtil.getCriteriaAllEQ(ms), null);
        if(count == 0){
            return;
        }else{
            long size =  count/50;
            for(int i = 0; i <= size ; i ++){
                QueryResult<MappingService> res = commonDao.findAll(MappingService.class, CriteriaUtil.getCriteriaAllEQ(ms), (i+1) , 50, null, null);
                if(res.getResultlist() != null && res.getResultlist().size() != 0){
                    List<MappingService> mdList = res.getResultlist();
                    for(int j = 0 ; j < mdList.size() ; j++){
                        MappingService ms1 = mdList.get(j);
                        String value = hostMap.get(ms1.getServiceType())+ms1.getValue();
                        redisUtil.set(ms1.getServiceCode(),value);
                    }
                }
            }
        }
    }
    /**
     * 获得mappingserviceHost

     * @throws Exception
     */
    @Deprecated
    private Map<String,String> _setMappingServiceHost()throws Exception{
        Map<String,String> resMap = new HashMap<>();
        MappingServiceHost ms = new MappingServiceHost();
        long count = commonDao.getCount(MappingServiceHost.class, CriteriaUtil.getCriteriaAllEQ(ms), null);
        if(count == 0){
            return resMap;
        }else{
            long size =  count/50;
            for(int i = 0; i <= size ; i ++){
                QueryResult<MappingServiceHost> res = commonDao.findAll(MappingServiceHost.class, CriteriaUtil.getCriteriaAllEQ(ms),(i+1) , 50, null, null);
                if(res.getResultlist() != null && res.getResultlist().size() != 0){
                    List<MappingServiceHost> mdList = res.getResultlist();
                    for(int j = 0 ; j < mdList.size() ; j++){
                        MappingServiceHost ms1 = mdList.get(j);
                        resMap.put(ms1.getServiceType(),ms1.getValue());
//                        redisUtil.set(ms.getServiceType(),ms.getValue());
                    }
                }
            }
        }
        return resMap;
    }

    /**
     * 集团信息
     * @return
     * @throws Exception
     */
    private void setL2RGroupBaseinfo()throws Exception{
        L2RGroupBaseinfo l2RGroupBaseinfo = new L2RGroupBaseinfo();
        long count = commonDao.getCount(L2RGroupBaseinfo.class, CriteriaUtil.getCriteriaAllEQ(l2RGroupBaseinfo), null);
        if(count == 0){
            return ;
        }else{
            long size =  count/50;
            for(int i = 0; i <= size ; i ++){
                QueryResult<L2RGroupBaseinfo> res = commonDao.findAll(L2RGroupBaseinfo.class, CriteriaUtil.getCriteriaAllEQ(l2RGroupBaseinfo),(i+1) , 50, null, null);
                if(res.getResultlist() != null && res.getResultlist().size() != 0){
                    List<L2RGroupBaseinfo> mdList = res.getResultlist();
                    for(int j = 0 ; j < mdList.size() ; j++){
                        L2RGroupBaseinfo l2RGroupBaseinfo1 = mdList.get(j);
                        L2RGroupBaseInfoDto l2RGroupBaseInfoDto=ClassConverUtil.copyPropertiesToDto(l2RGroupBaseinfo1, L2RGroupBaseInfoDto.class, false);
                        Optional.ofNullable(l2RGroupBaseinfo1.getPayOption()).ifPresent(value->l2RGroupBaseInfoDto.setL2RGroupPayOptionDto(JacksonUtil.jsonToBean(value, L2RGroupPayOptionDto.class)));
                        redisUtil.set(CommonUtil.getRedisKey(BaseConstant.RedisConstant.TITLE.HOTEL_GROUP,l2RGroupBaseinfo1.getLhotelGroupCode()),l2RGroupBaseInfoDto);
//                        redisUtil.set(ms.getServiceType(),ms.getValue());
                    }
                }
            }
        }
    }

    /**
     * 酒店信息
     * @return
     * @throws Exception
     */
    private void setL2RHotelBaseinfo()throws Exception{
        L2RHotelBaseinfo l2RHotelBaseinfo = new L2RHotelBaseinfo();
        long count = commonDao.getCount(L2RHotelBaseinfo.class, CriteriaUtil.getCriteriaAllEQ(l2RHotelBaseinfo), null);
        if(count == 0){
            return ;
        }else{
            long size =  count/50;
            for(int i = 0; i <= size ; i ++){
                QueryResult<L2RHotelBaseinfo> res = commonDao.findAll(L2RHotelBaseinfo.class, CriteriaUtil.getCriteriaAllEQ(l2RHotelBaseinfo),(i+1) , 50, null, null);
                if(res.getResultlist() != null && res.getResultlist().size() != 0){
                    List<L2RHotelBaseinfo> mdList = res.getResultlist();
                    for(int j = 0 ; j < mdList.size() ; j++){
                        L2RHotelBaseinfo hotelBaseinfo = mdList.get(j);
                        redisUtil.set(CommonUtil.getRedisKey(BaseConstant.RedisConstant.TITLE.HOTEL,hotelBaseinfo.getLhotelCode()), ClassConverUtil.copyPropertiesToDto(hotelBaseinfo, HotelBaseInfoDto.class, false));
//                        redisUtil.set(ms.getServiceType(),ms.getValue());
                    }
                }
            }
        }
    }

    /**
     * 加载mappingService
     * @throws Exception
     */
    private void setSysoptionService() throws Exception {
        SysOptions so = new SysOptions();
        long count = commonService.getCountAllEQ(SysOptions.class, so, null);
        if(count == 0){
            return;
        }else{
            long size =  count/50;
            for(int i = 0; i <= size ; i ++){
                QueryResult<SysOptions> soResult = commonService.findAllEQ(SysOptions.class, so, (i+1) , 50, null, null);
                if(soResult.getResultlist() != null && soResult.getResultlist().size() != 0){
                    List<SysOptions> soList = soResult.getResultlist();
                    for(int j = 0 ; j < soList.size() ; j++){
                        SysOptions so1 = soList.get(j);
                        redisUtil.set(so1.getLhotelGroupCode()+ DefaultValue.CUT+so1.getCategory()+DefaultValue.CUT+so1.getItem(),so1.getSetValue());
                    }
                }
            }
        }
    }

    private void setPmsReservationOptions()throws Exception{
        PmsReservationOption queryPRO=new PmsReservationOption();
        QueryResult<PmsReservationOption> pmsReservationOptionQueryResult=commonService.findAllEQ(PmsReservationOption.class,queryPRO);
        if(pmsReservationOptionQueryResult.getResultlist()!=null&&pmsReservationOptionQueryResult.getResultlist().size()>0){
            for(PmsReservationOption pmsReservationOption:pmsReservationOptionQueryResult.getResultlist()){
                PmsReservationOptionDto pmsReservationOptionDto = ClassConverUtil.copyPropertiesToDto(pmsReservationOption, PmsReservationOptionDto.class, false);
                if(StringUtil.isNotBlank(pmsReservationOption.getTaCode())){
                    pmsReservationOptionDto.setTaCodeMap(JacksonUtil.jsonToBean(pmsReservationOption.getTaCode(),Map.class));
                }
                redisUtil.set(BumblebeeUtil.getPmsReservationOptionKey(pmsReservationOption.getLhotelGroupCode(),
                        pmsReservationOption.getPbChannel(),
                        pmsReservationOption.getResType()),
                        pmsReservationOptionDto
                );
            }
        }
    }
}
